<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="heard :: head"></head>

<body>
<nav th:replace="heard :: nav"></nav>
<div class="container-fluid" style="font-size: 14px;">
    <div class="card">
        <div class="card-body ">
            <form id="formPD" style="font-size: 14px;">
                <input id="pid" name="parentId" type="text" style="display:none" th:value="${pid}">
                <input name="id" type="text" style="display:none" th:value="${pd?.id}">
                <div class="form-group row">
                    <label for="inputCode" class="col-sm-2 col-form-label">编码</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control form-control-sm" id="inputCode" name="code"
                               th:value="${pd?.code}"
                               placeholder="Email"/>
                    </div>

                    <label for="inputName" class="col-sm-2 col-form-label">名称</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control form-control-sm" id="inputName" name="name"
                               th:value="${pd?.name}"
                               placeholder=""/>
                    </div>
                </div>
                <div class="form-group row">
                    <label for="inputDescription" class="col-sm-2 col-form-label">描述</label>
                    <div class="col-sm-4">
                        <input type="text" class=" form-control form-control-sm" id="inputDescription"
                               name="description"
                               th:value="${pd?.description}">

                    </div>

                    <label for="inputdatatype" class="col-sm-2 col-form-label">数据类型</label>
                    <div class="col-sm-4">
                        <select class="custom-select form-control-sm" id="inputdatatype" name="datatype"
                                th:value="${pd?.datatype}">
                            <option selected value="1">String</option>
                            <option value="31">JSON</option>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <label for="inputdefaultValue" class="col-sm-2 col-form-label">缺省值</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control form-control-sm" id="inputdefaultValue"
                               name="defaultValue" th:value="${pd?.defaultValue}"
                               placeholder=""/>
                    </div>

                    <label for="inputformat" class="col-sm-2 col-form-label">格式</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control form-control-sm" id="inputformat" name="format"
                               th:value="${pd?.format}"
                               placeholder=""/>
                    </div>
                </div>

                <div class="form-group row">
                    <label for="inputallowedValues" class="col-sm-2 col-form-label">允许的值</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control form-control-sm" id="inputallowedValues"
                               name="allowedValues" th:value="${pd?.allowedValues}"
                               placeholder=""/>
                    </div>
                    <label class="col-sm-2 col-form-label">取值范围</label>
                    <div class="form-row form-inline col-md-4">
                        <div class="input-group  input-group-sm ">
                            <input type="text" class="form-control" id="inputminval" name="minval"
                                   th:value="${pd?.minval}">
                            <input type="text" class="form-control" id="inputmaxval" name="maxval"
                                   th:value="${pd?.maxval}">
                        </div>
                    </div>
                </div>

                <div class="form-group row">
                    <div class="form-group col-sm-3">
                        <div class="custom-control custom-switch">
                            <input type="checkbox" class="custom-control-input"
                                   id="inputfixed" name="fixed" th:value="${pd?.fixed}">
                            <label class="custom-control-label"
                                   for="inputfixed">是否固定</label>
                        </div>
                    </div>
                    <div class="form-group col-sm-3">
                        <div class="custom-control custom-switch">
                            <input type="checkbox" class="custom-control-input"
                                   id="inputnullable" name="nullable" th:value="${pd?.nullable}">
                            <label class="custom-control-label"
                                   for="inputnullable">是否可空</label>
                        </div>
                    </div>
                    <div class="form-group col-sm-3">
                        <div class="custom-control custom-switch">
                            <input type="checkbox" class="custom-control-input"
                                   id="inputAutoCache3" name="code">
                            <label class="custom-control-label"
                                   for="inputAutoCache3">是否允许变更</label>
                        </div>
                    </div>

                    <div class="form-group col-sm-3">
                        <a class="btn btn-primary btn-sm " href="javascript:window.history.back()">返回</a>
                        <button id="saveBth" class="btn btn-primary btn-sm" type="button">保存</button>
                    </div>
                </div>


            </form>
        </div>
    </div>

</div>


</body>
<script th:inline="javascript">

    $(function () {
        $("#saveBth").click(function () {
            var fields = $('#formPD').serializeArray();
            var obj = {}; //声明一个对象
            $.each(fields, function (index, field) {
                obj[field.name] = field.value; //通过变量，将属性值，属性一起放到对象中
            })
            $.ajax({
                type: "post",
                url: "/parameter/definition",
                async: true,
                contentType: 'application/json',
                dataType: 'JSON',
                data: JSON.stringify(obj),//将对象转为json字符串
                success: function (obj) {
                    let pid = $("#pid").val();
                    let id = obj.data;
                    window.location.href = '/home/p/' + pid + '/pd/';
                    //window.history.back()
                },
                error: function (obj) {
                    alert(obj.msg);
                }
            });
        })
    });
</script>
</html>
